home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / cd / Play / Play.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-17  |  4.6 KB  |  186 lines

  1. /* Play.c
  2.  *
  3.  * play specified cd tracks on cdgs.
  4.  * Control-C: abort and quit
  5.  * Control-F: skip (forward?) to next specified track.
  6.  *
  7.  * 1.1 - initial release
  8.  * 1.2 - added ^C/^F features
  9.  * 1.3 - added WaitIO() after CheckIO() for normal return.  Oops.
  10.  *
  11.  */
  12.  
  13. /* Includes --------------------------------------------- */
  14. #include <exec/types.h>
  15. #include <exec/libraries.h>
  16. #include <exec/memory.h>
  17. #include <exec/ports.h>
  18. #include <exec/io.h>
  19. #include <dos/dos.h>
  20. #include <clib/exec_protos.h>
  21. #include <clib/dos_protos.h>
  22. #include <devices/cd.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26.  
  27. /* Defines ------------------------------------------ */
  28. #define PROGNAME "Play"
  29. #define ERRMSG_LIBNOOPEN "Couldn't open %s V%ld (or better)!\n"
  30. #define TEMPLATE "TRACKS/M/N/A,NAME=DEVICENAME/K,UNIT/K/N"
  31. #define DEFAULT_UNIT_NAME    "cd.device"
  32. #define DEFAULT_UNIT_NUMBER  0UL
  33.  
  34. /* Structs ------------------------------ */
  35. struct Opts {
  36.     LONG   **tracks;   /* track numbers to play   */
  37.     STRPTR unitName;   /* name, default cd.device */
  38.     ULONG  *unitNum;   /* unit number, default 0  */
  39. };
  40.  
  41. /* Protos ------------------------------------------ */
  42. static VOID GoodBye(int);
  43. static LONG doInit(VOID);
  44. static BOOL playIt(struct IOStdReq *);
  45.  
  46. /* Globals --------------------------------------- */
  47. struct Opts          opts;
  48. struct RDArgs       *rdargs;
  49. struct IOStdReq     *io;
  50. struct MsgPort      *mp;
  51. LONG                 cdOpen;
  52.  
  53.  
  54. VOID main(int argc, UBYTE *argv[]) {
  55.     int i=0;
  56.  
  57.     if (!doInit()) {
  58.         GoodBye(RETURN_FAIL);
  59.     }
  60.  
  61.     io->io_Command = CD_PLAYTRACK;    /* Play audio tracks           */
  62.     io->io_Length =  1;               /* Play this many audio tracks */
  63.  
  64.     while (opts.tracks[i]) {
  65.         io->io_Data = NULL;
  66.         io->io_Offset = *opts.tracks[i];
  67.         if (!playIt(io)) {
  68.             WaitIO((struct IORequest *)io);  /* wait for cd to finish aborting */
  69.             break;
  70.         }
  71.         i++;
  72.     }
  73.  
  74.     GoodBye(RETURN_OK);
  75. }
  76.  
  77. /* playIt ===============================================
  78.    Play track asynchronously, allowing abort.
  79.    Returns:
  80.      TRUE means play the next track, done with the current one.
  81.      FALSE means quit the program.
  82.  */
  83. static BOOL playIt(struct IOStdReq *ios) {
  84.  
  85.     if (CheckSignal(SIGBREAKF_CTRL_C)) {
  86.         return(FALSE);
  87.     }
  88.  
  89.     /* send the io request */
  90.     SendIO((struct IORequest *)io);
  91.  
  92.     /* check the io request and loop */
  93.     while (!CheckIO((struct IORequest *)io)) {
  94.  
  95.         /* does user want to skip the rest of this track? */
  96.         if (CheckSignal(SIGBREAKF_CTRL_F)) {
  97.             PutStr("Continuing with next track...\n");
  98.             AbortIO((struct IORequest *)io);
  99.             WaitIO((struct IORequest *)io);
  100.             return(TRUE);
  101.         }
  102.  
  103.         /* does user want to quit out? */
  104.         if (CheckSignal(SIGBREAKF_CTRL_C)) {
  105.             PutStr("Aborting PlayList...\n");
  106.             AbortIO((struct IORequest *)io);
  107.             /* waitio is done by caller on False return */
  108.             return(FALSE);
  109.         }
  110.  
  111.         /* wait a bit over 0.5 sec before looking again */
  112.         Delay(30L);
  113.     }
  114.  
  115.     /* remove io from our queue... */
  116.     WaitIO((struct IORequest *)io);
  117.  
  118.     return(TRUE);
  119. }
  120.  
  121.  
  122. /* GoodBye ===============================================
  123.    Clean-exit routine.
  124.  */
  125. static VOID GoodBye(int rc) {
  126.  
  127.     if (rdargs) {
  128.         FreeArgs(rdargs);
  129.     }
  130.  
  131.     if (cdOpen) {
  132.         CloseDevice((struct IORequest *)io);
  133.     }
  134.     if (io) {
  135.         DeleteIORequest((struct IORequest *)io);
  136.     }
  137.     if (mp) {
  138.         DeleteMsgPort(mp);
  139.     }
  140.  
  141.     exit(rc);
  142. }
  143.  
  144. /* doInit =============================================
  145.  * Open libraries, call ReadArgs() if necessary.
  146.  * Returns TRUE for success, FALSE otherwise.
  147.  */
  148. static LONG doInit(VOID) {
  149.     STRPTR unitName;
  150.     ULONG  unitNum;
  151.  
  152.     rdargs = ReadArgs(TEMPLATE, (LONG *)&opts, NULL);
  153.     if (!rdargs) {
  154.         PrintFault(IoErr(), PROGNAME);
  155.         return(FALSE);
  156.     }
  157.     unitName = ((opts.unitName) ? opts.unitName : (STRPTR)DEFAULT_UNIT_NAME);
  158.     unitNum  = ((opts.unitNum) ? *opts.unitNum  : DEFAULT_UNIT_NUMBER);
  159.  
  160.     mp = CreateMsgPort();
  161.     if (!mp) {
  162.         PrintFault(IoErr(), PROGNAME);
  163.         PutStr("Couldn't create message port!\n");
  164.         return(FALSE);
  165.     }
  166.  
  167.     io = (struct IOStdReq *)CreateIORequest(mp, sizeof(struct IOStdReq));
  168.     if (!io) {
  169.         PrintFault(IoErr(), PROGNAME);
  170.         PutStr("Couldn't create IOStdRequest!\n");
  171.         return(FALSE);
  172.     }
  173.  
  174.  
  175.     if (OpenDevice(unitName, unitNum, (struct IORequest *)io, 0UL)) {
  176.         /* error was returned.  0 == success */
  177.         PrintFault(IoErr(), PROGNAME);
  178.         Printf("Couldn't open %s, unit %lu!\n", unitName, unitNum);
  179.         return(FALSE);
  180.     }
  181.     cdOpen = TRUE;
  182.  
  183.     return(TRUE);
  184. }
  185.  
  186.